package com.tsd.core.vo;

import com.tsd.core.constants.Constants;

import java.util.Map;

/**
 * 分页组件
 *
 * @author wudw
 */
public class PageBean {
    private int no = 1; // 页码,从1开始
    private int next; // 下页
    private int prev; // 上页
    private int pages; // 页数
    private int totalRows; // 总记录数据
    private int size = 15;// 每页记录数
    private int needCalPageCount = 1;//是否需要计算分页

    public PageBean() {

    }

    /**
     * 从map里面获取pageNo（页序号），和pageSize（页大小）
     *
     * @param map map
     */
    public PageBean(Map<String, Object> map) {
        Integer pageNo = getInt(map, Constants.KEY_PAGE_NO);
        if (pageNo != null) {
            this.no = pageNo;
        }
        Integer pageSize = getInt(map, Constants.KEY_PAGE_SIZE);
        if (pageSize != null) {
            this.size = pageSize;
        }
        Integer totalRows = getInt(map, Constants.KEY_TOTAL_ROWS);
        if (totalRows != null) {
            this.totalRows = totalRows;
        }
    }

    public PageBean(int no, int size) {
        this.no = no;
        this.size = size;
    }

    public PageBean procNoPage(Map<String, Object> map) {
        if (map.containsKey(Constants.KEY_NO_PAGE)) {
            return null;
        }
        return this;
    }

    public Integer getInt(Map<String, Object> map, String key) {
        if (map.containsKey(key)) {
            Object o = map.get(key);
            Integer val = null;
            if (o instanceof Integer) {
                val = (Integer) o;
            } else if (o instanceof String) {
                try {
                    val = Integer.parseInt((String) o);
                } catch (Exception ignored) {
                }
            }
            return val;
        } else {
            return null;
        }
    }

    public void recount() {
        pages = ((totalRows % size == 0) ? (totalRows / size)
                : (totalRows / size + 1));
        this.pages = pages == 0 ? 1 : pages;
        this.init();
    }

    public void init() {
        if (no <= 1) {
            if (no == 0) {
                no = 1;
            }
            prev = 1;
            next = no + 1;
        } else if (no >= pages) {
            no = pages;
            prev = no - 1;
            next = no + 1;
        } else {
            next = no + 1;
            prev = no - 1;
        }
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public int getNext() {
        return next;
    }

    public void setNext(int next) {
        this.next = next;
    }

    public int getPrev() {
        return prev;
    }

    public void setPrev(int prev) {
        this.prev = prev;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getTotalRows() {
        return totalRows;
    }

    /**
     * 设置总记录数并重新分页
     *
     * @param totalRows 总记录数并
     */
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getNeedCalPageCount() {
        return needCalPageCount;
    }

    public void setNeedCalPageCount(int needCalPageCount) {
        this.needCalPageCount = needCalPageCount;
    }
}
